<template>
  <el-dialog v-if="visible" v-model="visible" width="514px" :title="(supplierData.form.addressForm.supplierShippingId ? '修改' : '新增') + '退货地址'" :close-on-click-modal="false" :show-close="false" :close-on-press-escape="false">
    <el-form class="addressForm" :label-width="90" :model="supplierData.form.addressForm" ref="formRef">
      <el-form-item label="收货人：" prop="shippingPerson" :show-message="false" required>
        <el-input v-model="supplierData.form.addressForm.shippingPerson" class="third_input" placeholder="请输入名字" clearable></el-input>
      </el-form-item>
      <el-form-item label="联系电话:" prop="shippingMobile" :show-message="false" :rules="{ required: true, validator: validatePhone, trigger: 'blur' }">
        <el-input v-model="supplierData.form.addressForm.shippingMobile" class="third_input" placeholder="请输入联系电话" clearable></el-input>
      </el-form-item>
      <!-- <el-form-item label="所在地区" :required="true">
        <el-select v-model="supplierData.form.addressForm.shippingProvinceId" filterable placeholder="请选择(省,市)" clearable @change="onChange">
          <el-option v-for="item in supplierData.list" :label="item.provinceName" :value="item.provinceId" :key="item.provinceId"></el-option>
        </el-select>
        <el-input class="third_input" v-show="false" v-model="supplierData.form.addressForm.shippingProvince"></el-input>
      </el-form-item> -->
      <el-form-item label="详细地址:" prop="shippingAddressDetail" :show-message="false" required>
        <el-input v-model="supplierData.form.addressForm.shippingAddressDetail" type="textarea" resize="none" :rows="3" clearable placeholder="请填写详细地址"></el-input>
      </el-form-item>
      <div class="setDefault"><el-checkbox class="checked" label="设为默认收货地址" v-model="supplierData.form.addressForm.defaultFlag" true-label="YES" false-label="NO"></el-checkbox></div>
    </el-form>

    <template #footer>
      <Button label="取消" @click="onCancelClick" />
      <Button type="primary" label="保存" @click="onSubmitClick" />
    </template>
  </el-dialog>
</template>

<script setup>
import { reactive, computed, ref, toRefs, onMounted, nextTick } from 'vue'
import api from '@/api'
import { ElMessage } from 'element-plus'
import Button from '@/components/button/button.vue'

const props = defineProps(['visible', 'addressForm'])
const emit = defineEmits(['update:visible', 'change'])

const visible = computed({
  get: () => {
    return props.visible
  },
  set: (value) => emit('update:visible', value)
})
const { addressForm } = toRefs(props)

const supplierData = reactive({
  form: { addressForm },
  list: []
})
const formRef = ref()

const validatePhone = (rule, value, callback) => {
  const reg = /^1[3456789]\d{9}$/
  if (!value || !reg.test(value)) {
    callback(new Error())
  } else {
    callback()
  }
}

// const provinceData = async () => {
//   let { data, err } = await api.get('/medinstrument/third/province')
//   if (err) {
//     ElMessage.error(err)
//   } else {
//     supplierData.list = data.list
//   }
// }

const hide = () => {
  visible.value = false
  nextTick(() => {
    formRef.value.clearValidate()
  })
}

const save = async () => {
  let url = '/medinstrument/third/supplier/addShippingDetail'
  if (supplierData.form.addressForm.supplierShippingId) {
    url = '/medinstrument/third/supplier/updateShippingDetail'
  }
  let { err } = await api.post(url, supplierData.form.addressForm)
  if (err) {
    ElMessage.error(err)
  } else {
    emit('change')
    visible.value = false
  }
}

const onCancelClick = () => {
  visible.value = false
}
const onSubmitClick = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      save()
    }
  })
}

// const onChange = (val) => {
//   let provice = supplierData.list.find((e) => e.provinceId === val)
//   supplierData.form.addressForm.shippingProvince = provice.provinceName
// }

onMounted(() => {
  // provinceData()
})
</script>

<style scoped lang="scss">
.addressForm {
  .setDefault {
    margin-left: 90px;
  }
}
</style>
